#include "str.h"
#include <windows.h>

mztool::String::String(int interger)
{
    this->m_data = std::to_wstring(interger);
}

mztool::String::String(std::wstring str)
{
    this->m_data = str;
}

mztool::String::String(std::string str)
{
    this->m_data = String2WString(str);
}

std::wstring mztool::String::Append(std::wstring str)
{
    this->m_data.append(str);
    return this->m_data;
}

std::wstring mztool::String::Append(std::string str)
{
    this->Append(String2WString(str));
    return this->m_data;
}

int mztool::String::Length()
{
    return this->m_data.length();
}

bool mztool::String::Equals(String str)
{
    return this->Length() == str.Length() && this->m_data == str.ToWString();
}

std::string mztool::String::ToString()
{
    return WString2String(this->m_data);
}

std::wstring mztool::String::ToWString()
{
    return this->m_data;
}

std::string mztool::String::WString2String(const std::wstring &input)
{
    LPCWCH inStr = input.c_str();
    DWORD outLen = WideCharToMultiByte(CP_UTF8, 0, inStr, -1, NULL, 0, NULL, NULL);
    if (outLen <= 0) {
        return "";
    }
    char *outBuf = new char[outLen];
    WideCharToMultiByte(CP_UTF8, 0, inStr, -1, outBuf, outLen, NULL, NULL);
    std::string outStr(outBuf);
    delete outBuf;
    return outStr;
}

std::wstring mztool::String::String2WString(const std::string &input)
{
    LPCCH inStr = input.c_str();
    int inLen = (int)strlen(inStr);
    DWORD outLen = MultiByteToWideChar(CP_UTF8, 0, inStr, inLen, NULL, 0);
    if (outLen <= 0) {
        return L"";
    }
    wchar_t *outBuf = new wchar_t[outLen + 1];
    MultiByteToWideChar(CP_UTF8, 0, inStr, inLen, outBuf, outLen);
    outBuf[outLen] = 0;
    std::wstring outStr(outBuf);
    delete outBuf;
    return outStr;
}
